package cn.lg.soar.common.util;

import org.springframework.util.AntPathMatcher;

import java.util.Collection;

/**
 * 路径匹配器
 * @author luguoxiang
 * @date 2022/4/4
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class PathMatcher {

    private final static AntPathMatcher MATCHER = new AntPathMatcher();

    /**
     * 匹配
     * @param path 路径
     * @param pattern 模式
     * @return
     */
    public static boolean match(String path, String pattern) {
        return MATCHER.match(pattern, path);
    }

    /**
     * 任意一个匹配
     * @param path 路径
     * @param patterns 匹配模式
     * @return
     */
    public static boolean matchAny(String path, String...patterns) {
        for (String pattern : patterns) {
            if (MATCHER.match(pattern, path)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 任意一个匹配
     * @param path 路径
     * @param patterns 匹配模式
     * @return
     */
    public static boolean matchAny(String path, Collection<String> patterns) {
        return matchAny(path, patterns.toArray(new String[0]));
    }

    /**
     * 批量匹配
     * @param path 路径
     * @param patterns 匹配模式
     * @return
     */
    public static boolean[] batchMatch(String path, String...patterns) {
        boolean[] results = new boolean[ patterns.length ];
        for (int i = 0; i < results.length; i++) {
            results[i] = MATCHER.match(patterns[i], path);
        }
        return results;
    }

    /**
     * 批量匹配
     * @param path 路径
     * @param patterns 匹配模式
     * @return
     */
    public static boolean[] batchMatch(String path, Collection<String> patterns) {
        return batchMatch(path, patterns.toArray(new String[0]));
    }

}
